package com.fss.controller;

import com.fss.model.IndividualModel;
import com.fss.utils.MyCaptchaRender;
import com.jfinal.core.Controller;
import com.jfinal.ext.render.CaptchaRender;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class IndividualController extends Controller{
	private static final String RANDOM_CODE_KEY = "1"; 
	public void index(){
		render("login.jsp");
	}
	//生成验证码11
	 public void captcha()
 {
		 CaptchaRender img = new CaptchaRender(RANDOM_CODE_KEY); 
		 System.out.println("111111111111111");
	      render(img);
 }
	//拿着用户编号去查询作简单判断
	public void login(){
		String uidd=getPara("uid");
		int uid=Integer.parseInt(uidd);
		String password=getPara("password");
		String typeStaf=getPara("group1");
		System.out.println(uid+"密码是："+password);
		//验证码判断
		String inputRandomCode=getPara("randomCode");
		
		boolean loginSuccess = CaptchaRender.validate(this, inputRandomCode.toUpperCase(), RANDOM_CODE_KEY);
		
		if(!loginSuccess){
			//用户验证不正确3
			setAttr("status","4");
			System.out.println("验证码"+loginSuccess);
			renderJson();
			return;
		}
		Record userInfo=IndividualModel.DAO.getInfo(uid);
		//0 用户不存在 2密码错误1正确
		if(userInfo==null){	
			setAttr("status","0");
			renderJson();
		}
		else{
			
				if( userInfo.getStr("staff_type").equals(typeStaf)||userInfo.getStr("staff_type")==typeStaf){
					System.out.println("kayi"+userInfo);
				if(userInfo.getStr("staff_password").equals(password)){
					setAttr("status","1");
					this.getSession().setAttribute("loginUser", userInfo);
					renderJson();
				}
				else{
					setAttr("status","2");
					System.out.println("密码错误");
					renderJson();
				}
				}
				else{
					System.out.println("ka5456i"+userInfo);
					setAttr("status","3");//用户账户和类型不匹配
					renderJson();
				}
				
			}
				
		}
		

	public void userInfo(){
		
		Record loginuser = (Record) this.getSession().getAttribute("loginUser");
		setAttr("staff",loginuser);
		int typeSta=Integer.valueOf(loginuser.getStr("staff_type")).intValue();
		switch(typeSta)
		{
		 case 0:
			 System.out.println("11111");
             render("baseInfoStaff.jsp");//跳到员工信息显示界面
             break;
         case 1:
             System.out.println('b');
             break;
         case 2 :
             System.out.println('c');
             break;
			  
		}
		 
	return;
	}
    public void loginSuccess() {  
           
           render("index.jsp");
        }  
    public void  saveStaf(){
    	String sta_id=getPara("staId");
    	System.out.println("id"+sta_id);
    	String sex=getPara("sex");
    	String sta_bir=getPara("staBir");
    	String sta_add=getPara("address");
    	String sta_eml=getPara("emails");
    	String sta_tel=getPara("tel");
		if(IndividualModel.DAO.updateStaf(sta_id,sex,sta_bir,sta_add,sta_eml,sta_tel)){
			System.out.println("更新信息成功！");
			/*render("baseInfoStaff.jsp");*/
		}
		else{
			Record loginuser = (Record) this.getSession().getAttribute("loginUser");
			setAttr("staff",loginuser);
			setAttr("Tips","修改信息失败");
			render("baseInfoStaff.jsp");
		}
    }
}
